package org.jeecg.modules.PriceSystemModule.controller;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.PriceSystemModule.entity.PriceInervalEntity;
import org.jeecg.modules.PriceSystemModule.entity.PriceSystemEntity;
import org.jeecg.modules.PriceSystemModule.service.PriceManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RequestMapping("/PriceManagementController")
@RestController
public class PriceManagementController {
    @Autowired
    PriceManagementService service;
    /*
    查询售价管理列表
     */
    @GetMapping("/list")
    public Result<?> priceSystemList(
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "category", defaultValue = "0") Integer category,
            @RequestParam(name = "businessType", defaultValue = "10") Integer businessType,
            String queryStatus
    ) {
        Page<PriceSystemEntity> page = new Page<PriceSystemEntity>(pageNo, pageSize);
        IPage<PriceSystemEntity> pageList = service.getpriceSystemList(page,category,businessType,queryStatus);
        return Result.ok(pageList);
    }
    /*
    根据价格体系类别表的id查询价格区间数据
     */
    @GetMapping("/priceInervallist")
    public Result<?> priceInervallist(
            @RequestParam(name = "priceSystemId") String priceSystemId
    ) {
        List<PriceInervalEntity> list = service.getpriceInervallist(priceSystemId);
        return Result.ok(list);
    }
    /*
     假删除这条工艺类型价格区间数据
     */
    @GetMapping("/deletepriceInerval")
    public Result<?> deletepriceInerval(
            @RequestParam(name = "id") int id
    ) {
        try {
            int count = service.deletepriceInerval(id);
            return Result.ok("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ok("删除失败");
        }
    }
    /*
    为这个价格体系id添加一条区间数据
     */
    @PostMapping("/addpriceInerval")
    public Result<?> addpriceInerval(
            @RequestBody PriceInervalEntity entity
    ) {
        try {

            if(entity.getMinNumbers()>=entity.getMaxNumbers()){
                return Result.error("最大数小于等于最小数，请重写");
            }


             //判断一下这个价格体系的这个价格区间是否已经设置
            String oldcz=service.judgeoldcz(entity.getPriceSystemId(),entity.getMinNumbers(),entity.getMaxNumbers());
            if(oldcz!=null){
                return Result.error("该数字区间已经设置,请更改数字区间");
            }

             //获取阶梯列表，来循环拍断是否重复
            List<PriceInervalEntity> list=service.getPriceInervalEntitylist(entity.getPriceSystemId());
           if(list!=null){
               for (int i=0;i<list.size();i++){
                 if( entity.getMaxNumbers()>=list.get(i).getMinNumbers()&&entity.getMaxNumbers()<list.get(i).getMaxNumbers()){
                     return Result.error("所添加的阶梯数值重复，请修改");
                 }
                   if( entity.getMinNumbers()>=list.get(i).getMinNumbers()&&entity.getMinNumbers()<list.get(i).getMaxNumbers()){
                       return Result.error("所添加的阶梯数值重复，请修改");
                   }
               }
           }



            int count = service.addpriceInerval(entity);
            if(count>0)
                service.updatestatus(entity.getPriceSystemId());
            return Result.ok("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ok("请输入数字");
        }
    }
}
